<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>标签去重与排序</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f4f9;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            text-align: center;
            padding: 20px;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        input[type="text"] {
            width: 80%;
            padding: 10px;
            margin-bottom: 10px;
            border: 1px solid #ccc;
            border-radius: 5px;
        }

        button {
            padding: 10px 20px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 5px;
            cursor: pointer;
        }

        button:hover {
            background-color: #45a049;
        }

        .tags {
            margin-top: 20px;
            display: flex;
            flex-wrap: wrap;
            justify-content: center;
        }

        .tag {
            background-color: #e1e1e1;
            margin: 5px;
            padding: 8px;
            border-radius: 20px;
            font-size: 14px;
        }

        .tag span {
            margin-left: 10px;
            cursor: pointer;
            color: #d9534f;
        }
    </style>
</head>

<body>

    <div class="container">
        <h2>标签去重与排序</h2>
        <input type="text" id="tagInput" placeholder="输入标签" />
        <p class="tip">特别说明: 重复的标签名会自动去重</p>
        <button id="addTagButton">添加标签</button>

        <div class="tags" id="tagList">
            <!-- 标签会显示在这里 -->
        </div>
    </div>

    <script>
        // 标签存储和更新去重排序
        let tags = [];

        // 去重和排序函数
        const uniqueRegExp = arr =>
            arr.sort()
                .join(',,')
                .replace(/(,|^)([^,]+)(,,\2)+(,|$)/g, '\$1\$2\$4')
                .replace(/,,+/g, ',')
                .replace(/,$/, '')
                .split(',');

        // 添加标签函数
        document.getElementById('addTagButton').addEventListener('click', () => {
            const tagInput = document.getElementById('tagInput');
            const newTag = tagInput.value.trim();

            if (newTag) {
                tags.push(newTag);
                tags = uniqueRegExp(tags);
                displayTags();
                tagInput.value = ''; // 清空输入框
            }
        });

        // 渲染标签列表
        function displayTags() {
            const tagListElement = document.getElementById('tagList');
            tagListElement.innerHTML = '';

            tags.forEach(tag => {
                const tagElement = document.createElement('div');
                tagElement.classList.add('tag');
                tagElement.textContent = tag;

                // 创建删除按钮
                const deleteButton = document.createElement('span');
                deleteButton.textContent = '×';
                deleteButton.onclick = () => {
                    tags = tags.filter(t => t !== tag);
                    tags = uniqueRegExp(tags);
                    displayTags();
                };

                tagElement.appendChild(deleteButton);
                tagListElement.appendChild(tagElement);
            });
        }
    </script>

</body>

</html>